package com.leon.alishop.spider.util;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.IsEqualFilter;
import org.htmlparser.filters.TagNameFilter;

import com.leon.alishop.core.enumeration.TaobaoStoreTypeEnum;

public class NodeFilterUtil {

	/**
	 * 列表商品过滤
	 * @return
	 */
	public static NodeFilter getItemFilter(){
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("ul"),
				new HasAttributeFilter("class", "shop-list")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("a"),
				new HasAttributeFilter("class", "permalink"),
				new HasParentFilter(parentFilter,true)
		});
		return retFilter;
	}
	
	/**
	 * 列表商品过滤
	 * 淘宝2012版本
	 * @return
	 */
	public static NodeFilter getItemFilter(TaobaoStoreTypeEnum type){
		if(null == type 
				|| TaobaoStoreTypeEnum.VERSION_200X.equals(type))
			return getItemFilter();
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("dd"),
				new HasAttributeFilter("class", "detail")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("a"),
				new HasAttributeFilter("class", "item-name"),
				new HasParentFilter(parentFilter)
		});
		return retFilter;
	}
	
	
	/**
	 * 数字翻页过滤
	 * @return
	 */
	public static  NodeFilter getPageFilter(){
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("div"),
				new HasAttributeFilter("class", "pagination")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new HasParentFilter(parentFilter),
				new TagNameFilter("a"),
				new HasAttributeFilter("class", "J_SearchAsync")
		});
		return retFilter;
	}
	
	
	/**
	 * 下一页过滤
	 * @return
	 */
	public static  NodeFilter getNextPageFilter(){
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("div"),
				new HasAttributeFilter("class", "pagination")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("a"),
				new HasAttributeFilter("class", "J_SearchAsync next")
		});
		return retFilter;
	}
	
	/**
	 * 下一页过滤
	 * @return
	 */
	public static  NodeFilter getNextPageFilter(TaobaoStoreTypeEnum type){
		if(null == type 
				|| TaobaoStoreTypeEnum.VERSION_200X.equals(type))
			return getNextPageFilter();
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("div"),
				new HasAttributeFilter("class", "pagination")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("a"),
				new HasAttributeFilter("class", "J_SearchAsync next")
		});
		return retFilter;
	}
	
	
	/**
	 * 缩略图过滤
	 * @return
	 */
	public static  NodeFilter getThumbnailFilter(){
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("ul"),
				new HasAttributeFilter("id", "J_UlThumb")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new HasParentFilter(parentFilter,true),
				new TagNameFilter("img")
		});
		return retFilter;
	}
	
	/**
	 * 商品名称过滤
	 * @return
	 */
	public static  NodeFilter getGoodsNameFilter(){
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("div"),
				new HasAttributeFilter("class", "tb-detail-hd")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new HasParentFilter(parentFilter,true),
				new TagNameFilter("h3")
		});
		return retFilter;
	}
	
	/**
	 * 销量过滤
	 * @return
	 */
	public static  NodeFilter getTotalSaleVolumeFilter(){
		AndFilter retFilter = new AndFilter();
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("em"),
				new HasAttributeFilter("class", "J_TDealCount")
		});
		return retFilter;
	}
	
	/**
	 * 浏览量过滤
	 * @return
	 */
	public static NodeFilter getViewNumFilter(){
		AndFilter retFilter = new AndFilter(); 
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("em"),
				new HasAttributeFilter("id", "J_EmItemViews")
		});
		return retFilter;
	}
	
	/**
	 * 收藏量过滤
	 * @return
	 */
	public static NodeFilter getFavNumFilter(){
		AndFilter retFilter = new AndFilter(); 
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("em"),
				new HasAttributeFilter("id", "J_FavCount")
		});
		return retFilter;
	}
	
	/**
	 * 价格过滤
	 * @return
	 */
	public static  NodeFilter getGoodsPriceFilter(){
		AndFilter retFilter = new AndFilter();
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("strong"),
				new HasAttributeFilter("id", "J_StrPrice")
		});
		return retFilter;
	}
	/**
	 * 价格过滤
	 * @return
	 */
	public static  NodeFilter getGoodsPriceFilter(TaobaoStoreTypeEnum type){
		if(null == type || !TaobaoStoreTypeEnum.VERSION_2012.equals(type)){
			return getGoodsPriceFilter();
		}
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("li"),
				new HasAttributeFilter("id", "J_StrPriceModBox")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("em"),
				new HasAttributeFilter("class", "tb-rmb-num"),
				new HasParentFilter(parentFilter)
		});
		return retFilter;
	}
	
	/**
	 * 获取属性过滤
	 * @return
	 */
	public static  NodeFilter getAttrFilter(){
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("ul"),
				new HasAttributeFilter("class", "attributes-list")
		});
		retFilter.setPredicates(new NodeFilter[]{
				new HasParentFilter(parentFilter,true),
				new TagNameFilter("li")
		});
		return retFilter;
	}
	
	/**
	 * 获取大图过滤
	 * @return
	 */
	public static  NodeFilter getBigImgFilter(){
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("div"),
				new HasAttributeFilter("id", "J_DivItemDesc")
		});
		retFilter.setPredicates(new NodeFilter[]{
//				new HasParentFilter(parentFilter,true),
				new TagNameFilter("img")
		});
		return retFilter;
	}
	
	/**
	 * 获取 Script
	 * @return
	 */
	public static  NodeFilter getScriptFilter(){
		return new TagNameFilter("script");
	}
	
	/**
	 * 获取 SKU Size Tag
	 * @return
	 */
	public static  NodeFilter getSkuTagFilter(int id,int val){
		AndFilter retFilter = new AndFilter();
		AndFilter parentFilter = new AndFilter();
		parentFilter.setPredicates(new NodeFilter[]{
				new TagNameFilter("li"),
				new HasAttributeFilter("data-value", id + ":" + val)
		});
		retFilter.setPredicates(new NodeFilter[]{
				new HasParentFilter(parentFilter,true),
				new TagNameFilter("span")
		});
		return retFilter;
	}
	
	
}
